package com.example.mycnblog.controller;

import com.example.mycnblog.common.AjaxResult;
import com.example.mycnblog.common.UserSessionUtils;
import com.example.mycnblog.entity.Articleinfo;
import com.example.mycnblog.entity.Userinfo;
import com.example.mycnblog.service.ArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("/art")
public class ArticleController {

    @Autowired
    private ArticleService articleService;

    @RequestMapping("/mylist")
    public AjaxResult getMyList(HttpServletRequest request) {
        Userinfo userinfo = UserSessionUtils.getUser(request);
        if (userinfo == null) {
            return AjaxResult.fail(-1, "非法请求");
        }
        List<Articleinfo> list = articleService.getMyList(userinfo.getId());
        return AjaxResult.success(list);
    }

    @RequestMapping("/del")
    public AjaxResult del(Integer id, HttpServletRequest request) {
        if (id == null || id <= 0) {
            //参数有误
            return AjaxResult.fail(-1, "参数异常");
        }
        Userinfo userinfo = UserSessionUtils.getUser(request);
        if (userinfo == null) {
            return AjaxResult.fail(-2, "用户未登录");
        }
        return AjaxResult.success(articleService.del(id, userinfo.getId()));
    }

    @RequestMapping("/detail")
    public AjaxResult getDetail(Integer id) {
        if (id == null || id <= 0) {
            return AjaxResult.fail(-1, "非法参数");
        }
        return AjaxResult.success(articleService.getDetail(id));
    }

    @RequestMapping("/incr-rcount")
    public AjaxResult incrRCount(Integer id) {
        if (id != null && id > 0) {
            return AjaxResult.success(articleService.incrRCount(id));
        }
        return AjaxResult.success(-1, "未知错误");
    }

    @RequestMapping("/add")
    public AjaxResult add(HttpServletRequest request, Articleinfo articleinfo) {
        //1.非空校验
        if (articleinfo == null || !StringUtils.hasLength(articleinfo.getTitle()) || !StringUtils.hasLength(articleinfo.getContent())) {
            return AjaxResult.fail(-1, "非法参数");
        }
        //2.数据库添加操作
        Userinfo userinfo = UserSessionUtils.getUser(request);
        if (userinfo == null && userinfo.getId() <= 0) {
            return AjaxResult.fail(-2, "非法登录用户");
        }
        articleinfo.setUid(userinfo.getId());
        return AjaxResult.success(articleService.add(articleinfo));
    }

    @RequestMapping("/update")
    public AjaxResult update(HttpServletRequest request, Articleinfo articleinfo) {
        if (articleinfo == null || !StringUtils.hasLength(articleinfo.getTitle()) || !StringUtils.hasLength(articleinfo.getContent()) || articleinfo.getId() == null) {
            return AjaxResult.fail(-1, "非法参数");
        }
        Userinfo userinfo = UserSessionUtils.getUser(request);
        if (userinfo == null && userinfo.getId() <= 0) {
            return AjaxResult.fail(-2, "非法登录用户");
        }
        articleinfo.setUid(userinfo.getId());
        articleinfo.setUpdatetime(LocalDateTime.now());
        return AjaxResult.success(articleService.update(articleinfo));
    }

    @RequestMapping("/list")
    public AjaxResult getList() {
        List<Articleinfo> list = articleService.getList();
        return AjaxResult.success(list);
    }


    @RequestMapping("/listbypage")
    public AjaxResult getListByPage(Integer pindex, Integer psize) {
        //1.参数校正
        if (pindex == null || pindex <= 1) {
            pindex = 1;
        }
        if (psize == null || psize <= 1) {
            psize = 3;
        }
        //分页公式的值 =（当前页码-1）*每页显示条数
        int offset = (pindex - 1) * psize;
        //文章列表数据
        List<Articleinfo> list = articleService.getListByPage(psize, offset);
        //当前列表总共有多少页
        //a. 总共有多少条数据
        int totalCount = articleService.getCount();
        //b.总条数/psize
        double pcountdb = totalCount / (psize * 1.0);
        //c.使用 进一法 得到总页数
        int pcount=(int)Math.ceil(pcountdb);
        if(pindex>pcount){
            HashMap<String,Integer> result=new HashMap<>();
            result.put("pcount",pcount);
            return AjaxResult.fail(-1, "已经是最后一页",result);
        }
        HashMap<String,Object> result=new HashMap<>();
        result.put("list",list);
        result.put("pcount",pcount);
        return AjaxResult.success(result);

    }


}
